import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import LocalizeInputNumber from '../common/LocalizeInputNumber';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import ImportDealerPanel from './ImportDealerPanel';
import {hasPermissions} from './utils';
import {injectIntl} from './intl';
import Export from '../common/Export';
const FormItem = Form.Item;
class QueryPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        data: {},
        showDealerImportPanel: false
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    reset = () => {
        this.props.onResetClick();
    }
    onChangeDealerImport = () => {
        this.setState({
            showDealerImportPanel: true
        });
    }
    onCloseDealerImportPanel = () => {
        this.setState({
            showDealerImportPanel: false
        });
    }
    onImportDealerChange = data => {
        if(data)
            this.setState({
                showDealerImportPanel: false
            });
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onSelect({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const operations = [];
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        if(hasPermissions(this.props.permissions, PERMISSION.importDealer))
            operations.push(<Button key="importDealer" onClick={this.onChangeDealerImport}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.importDealer',
                defaultMessage: '库存导入'
            })}</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.export))
            operations.push(
                <Export key={PERMISSION.export} exportRequest={this.props.onClickExport} />
            );
        if(hasPermissions(this.props.permissions, PERMISSION.exportDetail))
            operations.push(
                <Export key={PERMISSION.exportDetail} exportRequest={this.props.onClickExportDetail} exportButton={this.props.intl.formatMessage({
                    id: 'queryPanel.btn.exportDetail',
                    defaultMessage: '明细导出'
                })}/>);
        return (
            <Form className="form-standard">
                <Row>
                    {!this.props.isDealerUser &&
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.dealerId',
                                defaultMessage: '总代'
                            })}
                            {...FORM_OPTIONS.item}>
                            <DealerSelectPanel
                                value={dealerInfo}
                                onSelect={this.onSelectDealer} />
                        </FormItem>
                    </Col>}
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.year',
                                defaultMessage: '库存年度'
                            })}
                            {...FORM_OPTIONS.item}>
                            <LocalizeInputNumber
                                name="year"
                                value={this.props.conditions.year}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.month',
                                defaultMessage: '库存月度'
                            })}
                            {...FORM_OPTIONS.item}>
                            <LocalizeInputNumber
                                name="month"
                                value={this.props.conditions.month}
                                onPressEnter={this.handleFilterPressEnter}
                                onBlur={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.reset}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {operations}
                    </Col>
                </Row>
                {
                    this.state.showDealerImportPanel &&
                    <ImportDealerPanel
                        selectKey={'import'}
                        onCancel={this.onCloseDealerImportPanel}
                        onImportChange={this.onImportDealerChange} />
                }
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    exportData,
    exportDetailData,
    saveQueryDealer,
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    conditions: getConditions(state),
    permissions: getPermissions(state),
    queryDealer: getQueryDealer(state),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(exportData()),
    onClickExportDetail: () => dispatch(exportDetailData()),
    onSelect: data => dispatch(saveQueryDealer(data)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
